home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: br34.bearriver.com!dmurphy
- From: Duane Murphy <dmurphy@bearriver.com>
- Subject: Suicidal Objects (was Aborting Constructors, Part II)
- Sender: usenet@BearRiver.com (Usenet News)
- Message-ID: <DM3sAp.52H@BearRiver.com>
- Date: Thu, 1 Feb 1996 15:28:01 GMT
- X-Xxdate: Thu, 1 Feb 1996 15:26:59 GMT
- X-Xxmessage-Id: <AD361BC316014E22@br34.bearriver.com>
- Content-Transfer-Encoding: 8bit
- Content-Type: text/plain; charset=ISO-8859-1
- References: <4eoeck$t6n@news.ios.com>
- Nntp-Posting-Host: br34.bearriver.com
- Mime-Version: 1.0
- Organization: Bear River Associates
- X-Newsreader: Nuntius 2.0.4_PPC
-
- Tilo Koerbs, mkt@isun04.inf.uni-jena.de writes:
- >> test::test(){
- >> cout << "constructing object\n";
- >> delete this;
- >> throw(xmsg(string("deleted the object!\n")));
- >> }
- >
- >I am sure that using delete on this is not allowed in C++.
-
- Sorry, calling delete on this is prefectly valid.
-
- Tilo Koerbs, mkt@isun04.inf.uni-jena.de also writes:
- >After the delete the object the function works with is
- >not valid any more. So what is the function working with?
-
- This part of your statement is true. The code doesnt magically go
- away, but the vtable does. You cannot call any other methods
- (actually only virtual methods; but its still dangerous at best)
-
- A previous response by Sergey was more correct.
-
- Calling delete only works if your object is heap based. There are
- ways of hiding constructors such that this is gauranteed, but it is
- odd behaviour. Furthermore, how is the client of the object to know
- that you just called delete?
-
- It is legal however to call the destructor as a function (I recall
- reading this somewhere, please correct me.)
-
- myclass::~myclass()
-
- This is equally dangerous as the client of the object still has not
- idea what happened.
-
- I missed your original post so I am not sure what you are trying to
- solve.
-
- A method that I use is to place cleanup code in a seperate
- function. The destructor calls this code, and the constructor can
- also call this code if need be.
-
- I am an exceptions enthusiast so my constructors will usually throw
- exceptions. There are other methods of getting feedback to object
- clients, but this seems fairly clean and simple.
-
- I hope this helps,
- ...Duane Murphy
- __________________________________________________________________________
- Duane Murphy Bear River Associates, Inc.
- <mailto:dmurphy@bearriver.com> <http://www.bearriver.com>
-